import {defineConfig} from 'vite'
import vue from '@vitejs/plugin-vue'
import path from "path";
// 去除console和debugger
import {terser} from 'rollup-plugin-terser';
import vueSetUpExtend from "vite-plugin-vue-setup-extend";
// 兼容低版本浏览器插件
import compression from 'vite-plugin-compression';

import AutoImport from 'unplugin-auto-import/vite'

import Components from 'unplugin-vue-components/vite'

import {VantResolver} from 'unplugin-vue-components/resolvers'

// vite.config.ts
import {defineConfig} from 'vite';
// @ts-ignore
export default defineConfig((ConfigEnv) => {
    const {mode, command} = ConfigEnv;
    // 定义默认配置
    const config = {
        base: "./",
        css: {
            loaderOptions: {
                stylus: {
                    import: [
                        path.join(__dirname, "./src/assets/css/variables.styl")
                    ]
                }
            }

        },
        plugins: [
            vue(),
            AutoImport({
                resolvers: [VantResolver()],
            }),
            Components({
                resolvers: [VantResolver()],
            }),
            compression({
                threshold: 10240 / 2, // 文件大小大于 10KB 才会被压缩
                algorithm: 'gzip', // 压缩算法，默认为 gzip
                deleteOriginFile: false, // 是否删除原文件，默认为 false
                ext: '.gz', // 压缩文件的扩展名，默认为 .gz
            }),
            terser(),
            vueSetUpExtend()
        ],
        build: {
            minify: 'terser',
            cssCodeSplit: true, // 分离 css
            rollupOptions: {
                output: {
                    assetFileNames: 'assets/[name]-[hash][extname]', // 打包后的静态资源路径
                },
            },
            terserOptions: {
                compress: {
                    keep_infinity: true,
                    drop_console: false,
                    drop_debugger: false
                }
            }
        },
        server: {
            host: "0.0.0.0",
            port: 9999,
            proxy: {
                '/api/': {
                    target: 'http://172.23.244.169:18200',
                    changeOrigin: true,
                    rewrite: (path) => path.replace(/^\/api/, '')
                },
                '/EnergyRiver': {
                    target: 'https://wws.energyriver.com',
                    changeOrigin: true,
                    rewrite: (path) => path.replace(/^\/EnergyRiver/, ''),
                    cookieDomainRewrite: {
                        "^/EnergyRiver": "/"
                    }
                },

            },
        },
        resolve: {
            alias: {
                // @ts-ignore
                "src": path.join(__dirname, "src"),
            }
        }
    };

    if (mode == "release") {
        config.build.terserOptions.compress.drop_console = true;
        config.build.terserOptions.compress.drop_debugger = true;
    }

    return config;
});
